package com.zby.business.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.zby.business.R;
import com.zby.business.adapter.ShopListViewAdapter;
import com.zby.business.net.MyAsyncTask;
import com.zby.business.util.App;
import com.zby.business.util.Util;

import org.json.JSONArray;
import org.json.JSONObject;

import java.nio.channels.Pipe;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

/**
 * Created by BY on 2016/3/6.
 */
public class Commodity extends Activity implements View.OnClickListener {
    private ListView commodityListView;
    private EditText searchEditText;
    private ImageView searchBackImageView;
    private TextView searchTextView;
    String productname = "", searchResult;
    private App app;
    private ArrayList commodityPrice, commodityName, commoditypicture, commodityIntroduce, commodityDetailId, commoditySale;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.commodity);
        initView();
        initData();
        search(getIntent().getExtras().get("name") + "");
        super.onCreate(savedInstanceState);
    }

    private void search(String name) {
        MyAsyncTask myAsyncTask = new MyAsyncTask();
        try {
            searchResult = myAsyncTask.execute("phoneSearch?name=" + name).get();
            System.out.println(searchResult);
            JSONObject commodityJsonObject = new JSONObject(searchResult);
            JSONArray commodityArray = commodityJsonObject.getJSONArray("commodity");
            if (commodityArray.length() > 0) {
                clearCommodity();
                for (int i = 0; i < commodityArray.length(); i++) {
                    JSONObject commodity = (JSONObject) commodityArray.get(i);
                    commodityDetailId.add(commodity.get("commodityDetailId"));
                    commodityName.add(commodity.get("commodityBrand"));
                    commodityPrice.add(commodity.get("commodityPrice"));
                    commoditypicture.add(commodity.get("commodityPicture"));
                    commodityIntroduce.add(commodity.get("commodityIntroduce"));
                    commoditySale.add(commodity.get("commoditySale"));
                }
                ShopListViewAdapter shopListViewAdapter = new ShopListViewAdapter(getApplicationContext(), app.getUserId(), commodityName, commoditypicture, commodityPrice,commodityDetailId);
                commodityListView.setAdapter(shopListViewAdapter);
            }
        } catch (Exception e) {
            e.printStackTrace();
            Util.toast(getApplicationContext(), "找不到,搜别的");
        }
    }

    private void clearCommodity() {
        commodityDetailId.clear();
        commodityName.clear();
        commodityPrice.clear();
        commoditypicture.clear();
        commodityIntroduce.clear();
        commoditySale.clear();
    }

    private void initData() {
        searchEditText.setText(getIntent().getExtras().get("name") + "");
//        ShopListViewAdapter shopListViewAdapter = new ShopListViewAdapter(getApplicationContext(), new String[]{"asd", "fsf", "saf"});
//        commodityListView.setAdapter(shopListViewAdapter);
    }

    private void initView() {
        app = (App) getApplication();
        commodityName = new ArrayList();
        commoditypicture = new ArrayList();
        commodityPrice = new ArrayList();
        commodityIntroduce = new ArrayList();
        commodityDetailId = new ArrayList();
        commoditySale = new ArrayList();
        commodityListView = (ListView) findViewById(R.id.commodityListView);
        searchBackImageView = (ImageView) findViewById(R.id.searchBack);
        searchEditText = (EditText) findViewById(R.id.searchEdit);
        searchTextView = (TextView) findViewById(R.id.searchText);
        searchBackImageView.setOnClickListener(this);
        searchTextView.setOnClickListener(this);
        searchEditText.setOnClickListener(this);
        commodityListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getApplicationContext(), Detail.class);
//                intent.putExtra("name", )
                Bundle bundle = new Bundle();
                bundle.putString("introduce", commodityIntroduce.get(position) + "");
                bundle.putString("price", commodityPrice.get(position) + "");
                bundle.putString("sale", commoditySale.get(position) + "");
                bundle.putString("commodityId", commodityDetailId.get(position) + "");
                bundle.putString("url", commoditypicture.get(position) + "");
                intent.putExtra("bundle", bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.searchBack:
                finish();
                break;
            case R.id.searchText:
                if (!searchEditText.getText().toString().equals("")) {
                    search(searchEditText.getText().toString());
                }
                break;
        }
    }
}
